{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9df067a4",
   "metadata": {},
   "source": [
    "## 读取csv和第一问，用csv的筛选"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b4db7685",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "87442\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import math\n",
    "from datetime import datetime\n",
    "csv_rout = \"./gaze.csv\"\n",
    "csv_data = pd.read_csv(csv_rout)\n",
    "csv_data2 = csv_data[csv_data['confidence']>=0.9]\n",
    "print(len(csv_data2))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "928a78ab",
   "metadata": {},
   "source": [
    "## 第二问，分别求x和y的平均值，标准差，都要满足3sigma才留下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "13e5cb1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "120026\n"
     ]
    }
   ],
   "source": [
    "ave_x = sum(csv_data['norm_pos_x'])/len(csv_data['norm_pos_x'])\n",
    "ave_y = sum(csv_data['norm_pos_y'])/len(csv_data['norm_pos_y'])\n",
    "x,y=0,0\n",
    "for i in range(len(csv_data['norm_pos_x'])):\n",
    "    x+=(csv_data['norm_pos_x'][i] - ave_x)**2\n",
    "for i in range(len(csv_data['norm_pos_y'])):\n",
    "    y+=(csv_data['norm_pos_x'][i] - ave_y)**2\n",
    "sigma_x = math.sqrt(x/len(csv_data['norm_pos_x']))\n",
    "sigma_y = math.sqrt(y/len(csv_data['norm_pos_y']))\n",
    "\n",
    "csv_data3 = csv_data[(csv_data['norm_pos_x']<=ave_x+3*sigma_x)&(csv_data['norm_pos_x']>=ave_x-3*sigma_x)&(csv_data['norm_pos_y']<=ave_y+3*sigma_y)&(csv_data['norm_pos_y']>=ave_y-3*sigma_y)]\n",
    "print(len(csv_data3))\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbc8a5e7",
   "metadata": {},
   "source": [
    "## 第三问 时间戳转化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "08a6d572",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0         1970-01-02 08:29:10+776780\n",
      "1         1970-01-02 08:29:10+776787\n",
      "2         1970-01-02 08:29:10+779709\n",
      "3         1970-01-02 08:29:10+779759\n",
      "4         1970-01-02 08:29:10+787485\n",
      "                     ...            \n",
      "125259    1970-01-02 08:34:03+695600\n",
      "125260    1970-01-02 08:34:03+697570\n",
      "125261    1970-01-02 08:34:03+701580\n",
      "125262    1970-01-02 08:34:03+703608\n",
      "125263    1970-01-02 08:34:03+705574\n",
      "Name: gaze_timestamp, Length: 125264, dtype: object\n"
     ]
    }
   ],
   "source": [
    "#t_lst = map(time.localtime,csv_data['gaze_timestamp'])\n",
    "pd.set_option('mode.chained_assignment', None)#关闭拷贝的警告\n",
    "csv_data4 = csv_data.copy()#因为每次都在原数据基础上修改，我不想改变原数据所以拷贝\n",
    "for i in range(len(csv_data)):\n",
    "    csv_data4['gaze_timestamp'][i]=str(datetime.fromtimestamp(csv_data['gaze_timestamp'][i])).replace('.','+')\n",
    "print(csv_data4['gaze_timestamp'])\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee0e0195",
   "metadata": {},
   "source": [
    "## 第四问 取三个点，看看这一秒的采样率，通过这秒数据个数来看"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5ec484c1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "取三个点来看采样率\n",
      "在08:29:12，采样率为426HZ\n",
      "在08:33:12，采样率为427HZ\n",
      "在08:34:02，采样率为427HZ\n"
     ]
    }
   ],
   "source": [
    "print('取三个点来看采样率')\n",
    "test = ['08:29:12','08:33:12','08:34:02']\n",
    "for i in test:\n",
    "    s = list(filter(lambda x: i in x,csv_data4['gaze_timestamp']))\n",
    "    print('在{}，采样率为{}HZ'.format(i,len(s)))\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0204919a",
   "metadata": {},
   "source": [
    "## 第五问 通过拿出每一个第八个数也就是每个0.01s的第一个数，来得到100HZ数据集，在从里边找到前二十个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0abe3a5d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                  base_data  confidence  eye_center0_3d_x  \\\n",
      "0                             88150.77678-0    0.828006         20.000000   \n",
      "4                            88150.787485-1    0.502595        -39.934928   \n",
      "7         88150.79569-0 88150.79147699999-1    0.625134         20.000000   \n",
      "10            88150.801157-0 88150.801123-1    0.708476         20.000000   \n",
      "15            88150.809749-0 88150.815997-1    0.856341         20.000000   \n",
      "19            88150.823534-0 88150.820104-1    0.861113         20.000000   \n",
      "24       88150.831679-0 88150.83159799999-1    0.792455         20.000000   \n",
      "28       88150.841019-0 88150.84344899999-1    0.861801         20.000000   \n",
      "32       88150.85153599999-0 88150.851534-1    0.814635         20.000000   \n",
      "37       88150.864506-0 88150.85956499999-1    0.865288         20.000000   \n",
      "41       88150.87172699999-0 88150.871726-1    0.779778         20.000000   \n",
      "45             88150.879586-0 88150.88389-1    0.945000         20.000000   \n",
      "50            88150.891699-0 88150.891702-1    0.923014         20.000000   \n",
      "54  88150.89951199999-0 88150.90357499999-1    0.821103         20.000000   \n",
      "58  88150.91139099999-0 88150.91137999999-1    0.929133         20.000000   \n",
      "63  88150.91942399999-0 88150.92752699999-1    0.856588         20.000000   \n",
      "66  88150.93622799999-0 88150.92752699999-1    0.998202         20.000000   \n",
      "70       88150.940894-0 88150.94089099999-1    0.739421         20.000000   \n",
      "75       88150.947523-0 88150.95548399999-1    0.803616         20.000000   \n",
      "79       88150.963521-0 88150.95945699999-1    0.839119         20.000000   \n",
      "\n",
      "    eye_center0_3d_y  eye_center0_3d_z  eye_center1_3d_x  eye_center1_3d_y  \\\n",
      "0          15.000000        -20.000000               NaN               NaN   \n",
      "4          14.997919        -20.075283               NaN               NaN   \n",
      "7          15.000000        -20.000000        -39.934928         14.997919   \n",
      "10         15.000000        -20.000000        -39.934928         14.997919   \n",
      "15         15.000000        -20.000000        -39.934928         14.997919   \n",
      "19         15.000000        -20.000000        -39.934928         14.997919   \n",
      "24         15.000000        -20.000000        -39.934928         14.997919   \n",
      "28         15.000000        -20.000000        -39.934928         14.997919   \n",
      "32         15.000000        -20.000000        -39.934928         14.997919   \n",
      "37         15.000000        -20.000000        -39.934928         14.997919   \n",
      "41         15.000000        -20.000000        -39.934928         14.997919   \n",
      "45         15.000000        -20.000000        -39.934928         14.997919   \n",
      "50         15.000000        -20.000000        -39.934928         14.997919   \n",
      "54         15.000000        -20.000000        -39.934928         14.997919   \n",
      "58         15.000000        -20.000000        -39.934928         14.997919   \n",
      "63         15.000000        -20.000000        -39.934928         14.997919   \n",
      "66         15.000000        -20.000000        -39.934928         14.997919   \n",
      "70         15.000000        -20.000000        -39.934928         14.997919   \n",
      "75         15.000000        -20.000000        -39.934928         14.997919   \n",
      "79         15.000000        -20.000000        -39.934928         14.997919   \n",
      "\n",
      "    eye_center1_3d_z  gaze_normal0_x  gaze_normal0_y  ...  gaze_normal1_x  \\\n",
      "0                NaN        0.011527        0.051002  ...             NaN   \n",
      "4                NaN       -0.136989        0.220475  ...             NaN   \n",
      "7         -20.075283        0.065145        0.027374  ...       -0.145087   \n",
      "10        -20.075283        0.067222        0.026625  ...       -0.152523   \n",
      "15        -20.075283        0.066338        0.023840  ...       -0.152094   \n",
      "19        -20.075283        0.063320        0.025199  ...       -0.151527   \n",
      "24        -20.075283        0.062870        0.027411  ...       -0.150210   \n",
      "28        -20.075283        0.061462        0.024975  ...       -0.150562   \n",
      "32        -20.075283        0.061283        0.025669  ...       -0.149817   \n",
      "37        -20.075283        0.060837        0.025458  ...       -0.149152   \n",
      "41        -20.075283        0.059950        0.025325  ...       -0.149645   \n",
      "45        -20.075283        0.060040        0.024936  ...       -0.149568   \n",
      "50        -20.075283        0.058560        0.024551  ...       -0.148916   \n",
      "54        -20.075283        0.058504        0.025053  ...       -0.149322   \n",
      "58        -20.075283        0.058121        0.024277  ...       -0.149559   \n",
      "63        -20.075283        0.057982        0.026013  ...       -0.148602   \n",
      "66        -20.075283        0.057256        0.025568  ...       -0.148602   \n",
      "70        -20.075283        0.057780        0.024715  ...       -0.148648   \n",
      "75        -20.075283        0.057914        0.025849  ...       -0.148821   \n",
      "79        -20.075283        0.057075        0.023795  ...       -0.149733   \n",
      "\n",
      "    gaze_normal1_y  gaze_normal1_z  gaze_point_3d_x  gaze_point_3d_y  \\\n",
      "0              NaN             NaN        28.369753        52.033135   \n",
      "4              NaN             NaN      -139.404860       175.088321   \n",
      "7         0.230744        0.962137         1.533100       -21.730862   \n",
      "10        0.222659        0.962891         1.782444       -18.930993   \n",
      "15        0.233980        0.960271         1.914321       -20.302480   \n",
      "19        0.234143        0.960321         2.451823       -21.099370   \n",
      "24        0.233747        0.960624         2.429748       -21.654815   \n",
      "28        0.233448        0.960642         2.740186       -21.448818   \n",
      "32        0.233036        0.960859         2.713785       -21.649325   \n",
      "37        0.234053        0.960715         2.747912       -21.958249   \n",
      "41        0.233258        0.960831         2.969824       -21.893036   \n",
      "45        0.232896        0.960931         2.944876       -21.783872   \n",
      "50        0.233733        0.960829         3.194857       -22.225656   \n",
      "54        0.234031        0.960694         3.240232       -22.277270   \n",
      "58        0.233748        0.960726         3.339173       -22.149845   \n",
      "63        0.234394        0.960717         3.288905       -22.693312   \n",
      "66        0.234394        0.960717         3.440890       -22.760252   \n",
      "70        0.234240        0.960747         3.334885       -22.510936   \n",
      "75        0.234277        0.960712         3.321167       -22.624493   \n",
      "79        0.234649        0.960479         3.571551       -22.364811   \n",
      "\n",
      "    gaze_point_3d_z  gaze_timestamp  norm_pos_x  norm_pos_y  world_index  \n",
      "0        705.121116    88150.776780    0.531433    0.397509          0.0  \n",
      "4        681.151453    88150.787485    0.340109    0.142989          1.0  \n",
      "7       -298.890441    88150.793583    0.495993    0.399021          1.0  \n",
      "10      -286.849309    88150.801140    0.495145    0.408338          1.0  \n",
      "15      -288.102445    88150.812873    0.494809    0.402125          2.0  \n",
      "19      -292.574406    88150.821819    0.493453    0.399839          2.0  \n",
      "24      -294.883347    88150.831638    0.493563    0.398007          2.0  \n",
      "28      -296.254320    88150.842234    0.492774    0.399444          2.0  \n",
      "32      -297.501283    88150.851535    0.492873    0.398930          3.0  \n",
      "37      -298.954997    88150.862036    0.492819    0.397986          3.0  \n",
      "41      -299.487170    88150.871726    0.492253    0.398470          3.0  \n",
      "45      -299.486159    88150.881738    0.492318    0.398976          4.0  \n",
      "50      -302.346860    88150.891700    0.491745    0.397902          4.0  \n",
      "54      -301.844557    88150.901543    0.491613    0.397495          4.0  \n",
      "58      -302.044727    88150.911385    0.491363    0.398149          5.0  \n",
      "63      -303.545591    88150.923475    0.491535    0.396166          5.0  \n",
      "66      -304.543969    88150.931877    0.491173    0.396201          5.0  \n",
      "70      -303.767708    88150.940892    0.491423    0.397075          5.0  \n",
      "75      -303.334427    88150.951503    0.491446    0.396408          6.0  \n",
      "79      -303.192161    88150.961489    0.490797    0.397549          6.0  \n",
      "\n",
      "[20 rows x 21 columns]\n"
     ]
    }
   ],
   "source": [
    "t = 'x'\n",
    "for i in range(len(csv_data['gaze_timestamp'])):\n",
    "    if t != str(csv_data['gaze_timestamp'][i])[7]:\n",
    "        csv_data4['gaze_timestamp'][i] = csv_data['gaze_timestamp'][i]\n",
    "        t = str(csv_data['gaze_timestamp'][i])[7]\n",
    "\n",
    "count = 0\n",
    "collection = []\n",
    "for i in range(len(csv_data4['gaze_timestamp'])):\n",
    "    if count == 20:\n",
    "        break\n",
    "    if str(csv_data4['gaze_timestamp'][i])[0] == '8':\n",
    "        count+=1\n",
    "        collection.append(i)\n",
    "csv_data5 = pd.DataFrame()\n",
    "for j in collection:\n",
    "    csv_data5 = csv_data5.append(csv_data4.loc[j])\n",
    "print(csv_data5)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2cb49a53",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
